package com.cw.demo.dao.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.cw.demo.dao.SalarygradeDao;
import com.cw.demo.model.Salarygrade;
import com.cw.demo.utils.DbUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository //等价于bean
public class SalarygradeDaoImpl implements SalarygradeDao {

    @Autowired
    private DruidDataSource dataSource;
    public boolean saveSalarygrade(Salarygrade salarygrade){
       // Connection connection = DbUtils.getInstance().getConnection();
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("insert into salarygrade(grade, losalary, hisalary) values (?,?,?)");
            preparedStatement.setInt(1,salarygrade.getGrade());
            preparedStatement.setInt(2,salarygrade.getLosalary());
            preparedStatement.setInt(3,salarygrade.getHisalary());
            preparedStatement.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }

    public boolean updateSalarygrade(Salarygrade salarygrade){

        //Connection connection = DbUtils.getInstance().getConnection();
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("update salarygrade set losalary = ? , hisalary = ? where  grade = ?");

            preparedStatement.setInt(1,salarygrade.getLosalary());
            preparedStatement.setInt(2,salarygrade.getHisalary());
            preparedStatement.setInt(3,salarygrade.getGrade());
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteSalarygrade(int grade){
       // Connection connection = DbUtils.getInstance().getConnection();
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("delete salarygrade  job where  grade = " + grade);
            preparedStatement.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }

    /**
     * 查询操作。根据查询条件  dname和loc进行模糊查询
     * @param param
     * @return
     */
   public List<Salarygrade> findByParam(Salarygrade param){
        String sql = "select * from salarygrade where 1 = 1";
        //字符串的操作工具类：可以帮助判断字符串是否为null或''
        if(param != null){
            if(StringUtils.isNotEmpty(String.valueOf(param.getLosalary()))){
                sql += " and dname like '%"+param.getLosalary()+"%'";
            }
            if (StringUtils.isNotEmpty(String.valueOf(param.getHisalary()))){
                sql += " and loc like '%"+param.getHisalary()+"%'";
            }
        }

        //将查询结果进行重新封装，返回集合bean对象
       // Connection connection = DbUtils.getInstance().getConnection();
       Connection connection = null;
       try {
           connection = dataSource.getConnection();
       } catch (SQLException e) {
           e.printStackTrace();
       }
        //创建一个返回结果集类型的空对象
        List<Salarygrade> list = new ArrayList<Salarygrade>();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Salarygrade salarygrade = new Salarygrade();
                salarygrade.setGrade(resultSet.getInt("grade"));
                salarygrade.setLosalary(resultSet.getInt("losalary"));
                salarygrade.setHisalary(resultSet.getInt("hisalary"));

                list.add(salarygrade);
            }
//            return list;
        } catch (SQLException e) {
            e.printStackTrace();
//            return list;
        }
        return list;
    }


    /**
     * 根据主键ID查询对象信息
     * @param id
     * @return
     */
    public Salarygrade findById(int grade){
        //Connection connection = DbUtils.getInstance().getConnection();
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("select  * from salarygrade where  grade = " + grade);
            ResultSet resultSet = preparedStatement.executeQuery();//此时，数据返回的肯定只有一条或0条
            Salarygrade salarygrade = new Salarygrade();
            boolean result = resultSet.next();
            if (result){
                salarygrade.setGrade(resultSet.getInt("grade"));
                salarygrade.setLosalary(resultSet.getInt("losalary"));
                salarygrade.setHisalary(resultSet.getInt("hisalary"));
            }
            return salarygrade;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }


    }

    @Bean
    public Salarygrade initSalarygrade(){
        Salarygrade salarygrade = new Salarygrade();
        salarygrade.setGrade(1001);
        salarygrade.setLosalary(1002);
        salarygrade.setHisalary(1003);
        return salarygrade;
    }
}
